package com.ailanyin.modules.system.controller;

import com.ailanyin.common.base.domain.BaseQuery;
import com.ailanyin.common.base.result.AjaxResult;
import com.ailanyin.common.database.result.CommonPage;
import com.ailanyin.common.log.annotation.Log;
import com.ailanyin.common.log.enums.BusinessType;
import com.ailanyin.modules.system.domain.SysNotice;
import com.ailanyin.modules.system.service.ISysNoticeService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController {
    @Autowired
    private ISysNoticeService noticeService;

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("hasAuthority('system:notice:list')")
    @GetMapping("/list")
    public CommonPage list(SysNotice notice,
                           @RequestParam(value = "pageSize" , defaultValue = "5") Integer pageSize,
                           @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return CommonPage.restPage(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("hasAuthority('system:notice:query')")
    @GetMapping
    public AjaxResult getInfo(@RequestParam("noticeId") Long noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("hasAuthority('system:notice:add')")
    @Log(title = "通知公告" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNotice notice) {
        return AjaxResult.toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("hasAuthority('system:notice:edit')")
    @Log(title = "通知公告" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNotice notice) {
        return AjaxResult.toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("hasAuthority('system:notice:remove')")
    @Log(title = "通知公告" , businessType = BusinessType.DELETE)
    @DeleteMapping
    public AjaxResult remove(@RequestBody BaseQuery<Long> query) {
        return AjaxResult.toAjax(noticeService.deleteNoticeByIds(query.getIds()));
    }
}
